package com.hawk.eye.server.controller.hawkeye;

import com.hawk.eye.common.core.ApiResult;
import com.hawk.eye.framework.utils.CollectionUtil;
import com.hawk.eye.common.page.PageQuery;
import com.hawk.eye.common.page.PageResult;
import com.hawk.eye.server.convert.RepairApiConvert;
import com.hawk.eye.server.convert.RepairApiLogConvert;
import com.hawk.eye.server.domain.dto.resp.RepairApiLogRespDTO;
import com.hawk.eye.server.domain.dto.resp.RepairApiRespDTO;
import com.hawk.eye.server.domain.vo.req.RepairApiReqVO;
import com.hawk.eye.server.domain.vo.resp.RepairApiLogRespVO;
import com.hawk.eye.server.domain.vo.resp.RepairApiParamRespVO;
import com.hawk.eye.server.domain.vo.resp.RepairApiRespVO;
import com.hawk.eye.server.service.IRepairApiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 修复脚本 Controller
 *
 * @author salad
 * @date 2023-09-23
 */
@Slf4j
@RestController
@RequestMapping("/hawk-eye/repair")
@RequiredArgsConstructor
public class RepairApiController {

    private final IRepairApiService repairApiService;

    /**
     * 分页查询修复脚本
     * @param pageQuery
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair:query')")
    @GetMapping("/page")
    public PageResult<RepairApiRespVO> page(PageQuery pageQuery, RepairApiReqVO reqVO) {
        PageResult<RepairApiRespDTO> pageResult = repairApiService.page(pageQuery, RepairApiConvert.INSTANCE.to(reqVO));
        return PageResult.of(pageResult, RepairApiConvert.INSTANCE::to);
    }

    /**
     * 新增修复脚本
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair:add')")
    @PostMapping("/save")
    public ApiResult save(@RequestBody @Validated RepairApiReqVO reqVO) {
        boolean status = repairApiService.save(RepairApiConvert.INSTANCE.to(reqVO));
        return ApiResult.status(status);
    }

    /**
     * 修复脚本详情
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair:query')")
    @GetMapping(value = "detail/{id}")
    public ApiResult detail(@PathVariable("id") Long id) {
        return ApiResult.ok(RepairApiConvert.INSTANCE.to(repairApiService.detail(id)));
    }

    /**
     * 修改修复脚本
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair:edit')")
    @PutMapping("/update")
    public ApiResult update(@RequestBody @Validated RepairApiReqVO reqVO) {
        return ApiResult.status(repairApiService.update(RepairApiConvert.INSTANCE.to(reqVO)));
    }

    /**
     * 删除修复脚本
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair:remove')")
    @DeleteMapping("/remove/{ids}")
    public ApiResult remove(@PathVariable Long[] ids) {
        return ApiResult.status(repairApiService.removeBatchByIds(CollectionUtil.toList(ids)));
    }

    /**
     * 获取修复脚本执行参数
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair:query')")
    @GetMapping(value = "execute-repair-params/{id}")
    public ApiResult<RepairApiParamRespVO> getExecuteRepairParams(@PathVariable("id") Long id) {
        return ApiResult.ok(RepairApiConvert.INSTANCE.to2(repairApiService.getExecuteRepairParams(id)));
    }

    /**
     * 执行修复脚本
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair:edit')")
    @PostMapping(value = "execute-repair")
    public ApiResult executeRepairApi(@RequestBody RepairApiReqVO reqVO) {
        return ApiResult.ok(repairApiService.executeRepairApi(RepairApiConvert.INSTANCE.to(reqVO)));
    }

    /**
     * 查询修复脚本执行日志
     * @param repairId
     * @param pageQuery
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair:query')")
    @GetMapping(value = "repair-log-list")
    public PageResult<RepairApiLogRespVO> repairApiLogList(@RequestParam("repairId") Long repairId, PageQuery pageQuery) {
        PageResult<RepairApiLogRespDTO> pageResult = repairApiService.repairApiLogList(repairId, pageQuery);
        return PageResult.of(pageResult, RepairApiLogConvert.INSTANCE::to);
    }

}
